package com.chuangke.sinotrukbudget.manager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.chuangke.admin.model.SysDataPermission;
import com.chuangke.admin.model.SysDept;
import com.chuangke.admin.model.SysDict;
import com.chuangke.admin.model.SysUser;
import com.chuangke.admin.service.SysDataPermissionService;
import com.chuangke.admin.service.SysDeptService;
import com.chuangke.admin.service.SysDictService;
import com.chuangke.admin.service.SysUserService;
import com.chuangke.common.utils.SecurityUtils;
import com.chuangke.framework.model.Record;
import com.chuangke.olap.model.Property;
import com.chuangke.sinotrukbudget.consts.BudgetContanst;
import com.chuangke.sinotrukbudget.model.BudgetAccount;
import com.chuangke.sinotrukbudget.model.BudgetDeptAccount;
import com.chuangke.sinotrukbudget.service.BudgetAccountService;
import com.chuangke.sinotrukbudget.service.BudgetDeptAccountService;

@Component
public class BudgetManager {

	@Autowired
	private BudgetAccountService budgetAccountService;
	@Autowired
	private SysDeptService sysDeptService;
	@Autowired
	private SysUserService sysUserService;
	@Autowired
	private SysDictService sysDictService;
    @Autowired
	public BudgetDeptAccountService budgetDeptAccountService;
	@Autowired
	public SysDataPermissionService sysDataPermissionService;

	public List<Property> getProperty(Map<String, String> propertyNameMap) {
		// 科目
		Map<String, BudgetAccount> accountMap = budgetAccountService.findMap();
		Map<String, String> accountMetaData = accountMap.values().stream()
				.collect(Collectors.toMap(BudgetAccount::getId, BudgetAccount::getName));
		Map<String, Record> standardData = budgetAccountService.getStandardData();

		// 组织机构
		Map<String, SysDept> deptMap = sysDeptService.findMap();
		Map<String, String> deptMetaData = deptMap.values().stream()
				.collect(Collectors.toMap(SysDept::getId, SysDept::getName));
		Map<String, Record> deptData = sysDeptService.getStandardData();

		// 组织机构
		Map<String, SysUser> userMap = sysUserService.findMap();
		Map<String, String> userMetaData = userMap.values().stream()
				.collect(Collectors.toMap(SysUser::getId, SysUser::getNickName));

		// flow
//		Map<String, String> flowMetaData = sysDictService.find("flow").values().stream()
//				.collect(Collectors.toMap(SysDict::getId, SysDict::getName));
		Map<String, String> flowMetaData = new LinkedHashMap<>();
		flowMetaData.put("6602", "管理费用");
		flowMetaData.put("6601", "销售费用");
		flowMetaData.put("5101", "制造费用");
		flowMetaData.put("5301", "研发支出");

		// flow1
		Map<String, String> flowMetaData1 = sysDictService.findMap("flow1").values().stream()
				.collect(Collectors.toMap(SysDict::getCode, SysDict::getName));
		Map<String, Record> flow1Data = sysDictService.getStandardData("flow1");
		// flow2
		Map<String, String> flowMetaData2 = sysDictService.findMap("flow2").values().stream()
				.collect(Collectors.toMap(SysDict::getCode, SysDict::getName));
		Map<String, Record> flow2Data = sysDictService.getStandardData("flow2");
		// flow3
		Map<String, String> flowMetaData3 = sysDictService.findMap("flow3").values().stream()
				.collect(Collectors.toMap(SysDict::getCode, SysDict::getName));
		Map<String, Record> flow3Data = sysDictService.getStandardData("flow3");
		// flow4
		Map<String, String> flowMetaData4 = sysDictService.findMap("flow4").values().stream()
				.collect(Collectors.toMap(SysDict::getCode, SysDict::getName));
		Map<String, Record> flow4Data = sysDictService.getStandardData("flow4");
		// flow5
		Map<String, String> flowMetaData5 = sysDictService.findMap("flow5").values().stream()
				.collect(Collectors.toMap(SysDict::getCode, SysDict::getName));
		Map<String, Record> flow5Data = sysDictService.getStandardData("flow5");
		// flow6
		Map<String, String> flowMetaData6 = sysDictService.findMap("flow6").values().stream()
				.collect(Collectors.toMap(SysDict::getCode, SysDict::getName));
		Map<String, Record> flow6Data = sysDictService.getStandardData("flow6");

		// budget_time
		Map<String, String> budgetTime = sysDictService.findMap("budget_time").values().stream()
				.collect(Collectors.toMap(SysDict::getCode, SysDict::getName));

		Map<String, Property> map = new HashMap<>();
		map.put("id", new Property("id", "id", "D"));
		map.put("submitter", new Property("submitter", "上报者", "D",userMetaData));
		map.put("t_entity", new Property("t_entity", "单位", "D", deptMetaData, deptData));
		map.put("t_category", new Property("t_category", "预算版本", "D"));
		map.put("t_audit", new Property("t_audit", "审计线索", "D"));
		map.put("t_account", new Property("t_account", "预算科目", "D", accountMetaData, standardData));
		map.put("t_time", new Property("t_time", "预算期间", "D", budgetTime));
		map.put("t_flow", new Property("t_flow", "功能范围", "D", flowMetaData));
		map.put("t_flow1", new Property("t_flow1", "预算事项", "D", flowMetaData1, flow1Data));
		map.put("t_flow2", new Property("t_flow2", "预算产品", "D", flowMetaData2, flow2Data));
		map.put("t_flow3", new Property("t_flow3", "关联方", "D", flowMetaData3, flow3Data));
		map.put("t_flow4", new Property("t_flow4", "业务项目", "D", flowMetaData4, flow4Data));
		map.put("t_flow5", new Property("t_flow5", "预留", "D",flowMetaData5, flow5Data));
		map.put("t_flow6", new Property("t_flow6", "预留", "D", flowMetaData6, flow6Data));
		map.put("t_object", new Property("t_object", "预留", "D"));
		map.put("signdata", new Property("signdata", "数值", "M"));
		map.put("status", new Property("status", "状态", "D"));
		map.put("peixun_type", new Property("peixun_type", "培训类型", "D", flowMetaData1));

		for (String rw : propertyNameMap.keySet()) {
			map.get(rw).setName(propertyNameMap.get(rw));
		}

		return new ArrayList<>(map.values());
	}

	public List<String> getGuikoukm() {
		String userId = SecurityUtils.getUserId();
	
		List<SysDataPermission> sdpList = sysDataPermissionService.find(SysDataPermission.TYPE_USER_DUTY);
		if (CollectionUtils.isEmpty(sdpList)) {
			return new ArrayList<>();
		}
	
		List<String> deptIdList = sdpList.stream()
				.filter(sdp -> userId.equals(sdp.getMaster1Id())
						&& BudgetContanst.BUDGET_FLOW_ROLE_NORMAL_ID.equals(sdp.getMaster2Id()))
				.map(SysDataPermission::getDataId).collect(Collectors.toList());
		if (CollectionUtils.isEmpty(sdpList)) {
			return new ArrayList<>();
		}
	
		List<BudgetDeptAccount> bdaList = budgetDeptAccountService.listByDepts(deptIdList);
		if (CollectionUtils.isEmpty(bdaList)) {
			return new ArrayList<>();
		}
	
		return bdaList.stream().map(BudgetDeptAccount::getAccountId).collect(Collectors.toList());
	}
}
